rem APP Hexdump
rem 	TYPE $1000
rem 	ICON "m:\app\utils3.ibx"	              
rem ENDA
PROC hexdump:
	global count%
	local ret%,fname$(128),txt$(255),address%
	local handle%,mode%,k%,data%, lastkey%
	local n%, j%, d%
		
	rem Get the name of the file to be dumped

	fname$="m:\agn\*.*"
	dINIT "Dateiauswahl"
	dFILE fname$, "Dateiname",16
	dBUTTONS "Abbruch",-27,"OK",13
	d%=DIALOG
	IF d% <= 0
		return
	endif

	font 1, 16			REM set the small font
	count%=0				REM total nr bytes 
	
	REM Set up and open file
	
	mode%=0
	ret%=ioopen(handle%,fname$,mode%)
	if ret% < 0
		ShowErr:(ret%)
		return
	endif
	address% =addr(data%)
	
	REM Loop on single byte reads 
	REM until EOF or Esc pressed
	
	lastkey%=key
	while lastkey%=0 
		ret%=ioread(handle%,address%+1,1)
		if ret% < 0
			if ret% <> -36		REM EOF()  ..(?)
				ShowErr:(ret%)
			endif
			break
		endif
		if ret% <> 1		REM endure 1 byte read
			print "Err: ret% <> 1"
			get
			return
		endif
		
		REM use only left half of integer
		
		data% = ((data% and $ff00) / 256) and $000000ff
		PHex:(data%) 
		count% = count% + 1		REM count the byte
		lastkey%=key					REM check for key
	endwh
	ret%=ioclose(handle%)
	if lastkey%=0
		get			REM final wait if no Esc pressed
	endif
ENDP
PROC PHex:(d%)
	local d$(2), x%, row%, a$(5)
	
	REM calc row for display current byte
	
	row%=1+mod%:(count%,256)/16
	if mod%:(count%, 256) > 127
		row% = row% + 1	REM blank line vert in middle
	endif
	if mod%:(count%,256)=0 and count% > 0
		get :cls				REM pause/end of screen
	endif
	if mod%:(count%, 16)=0 
		a$=hex$(count%)
		while len(a$) < 5
			a$="0"+a$
		endwh
		at 1, row%			REM offset at left of line
		print a$;
	endif
	x%=8+mod%:(count%*3, 48)
	if mod%:(count%, 16) > 7
		x%=x%+2					REM horiz. pos. in line
	endif
	d$=hex$(d%)
	if len(d$) < 2
		d$ = "0" + d$		REM leading zero if < $F
	endif
  at x%, row%
	print d$;
	
	if d%<>12 and d%<>7	REM non-displayable chars
		at 60+mod%:(count%, 16), row%
		print chr$(d%)
	else
		print "."
	endif
ENDP
PROC ShowErr:(val%)
	print "Error",val%,err$(val%)
	get
ENDP
PROC mod%:(v1%, v2%)
	return v1% - v1% / v2% * v2%
ENDP

